import { getWarningMsg, getWarningNum } from '@/api/modules/alarm'

export const useNotificationStore = defineStore(
  'notification',
  () => {
    const [generalCount, setGeneralCount] = useState(0)
    const [seriousCount, setSeriousCount] = useState(0)
    const [urgentCount, setUrgentCount] = useState(0)
    const [infoList, setInfoList] = useState<{ time: string, content: string, warningCategoryName: string }[]>([])
    const [visible, setVisible] = useState(false)

    const userStore = useUserStore()

    const generalNumObj = shallowReactive({
      ybWclNum: 0,
      ybClzNum: 0,
      ybDshNum: 0,
      ybYclNum: 0,
      ybYhlNum: 0,
    })
    const seriousNumObj = shallowReactive({
      yzWclNum: 0,
      yzClzNum: 0,
      yzDshNum: 0,
      yzYclNum: 0,
      yzYhlNum: 0,
    })
    const urgentNumObj = shallowReactive({
      jjWclNum: 0,
      jjClzNum: 0,
      jjDshNum: 0,
      jjYclNum: 0,
      jjYhlNum: 0,
    })
    const generalTotal = computed(() => {
      const { ybWclNum, ybClzNum, ybDshNum, ybYclNum, ybYhlNum } = generalNumObj
      return ybWclNum + ybClzNum + ybDshNum + ybYclNum + ybYhlNum
    })
    const seriousTotal = computed(() => {
      const { yzWclNum, yzClzNum, yzDshNum, yzYclNum, yzYhlNum } = seriousNumObj
      return yzWclNum + yzClzNum + yzDshNum + yzYclNum + yzYhlNum
    })
    const urgentTotal = computed(() => {
      const { jjWclNum, jjClzNum, jjDshNum, jjYclNum, jjYhlNum } = urgentNumObj
      return jjWclNum + jjClzNum + jjDshNum + jjYclNum + jjYhlNum
    })

    async function updateNotificationCount() {
      try {
        const { data } = await getWarningNum()
        const {
          ybWclNum: general,
          yzWclNum: serious,
          jjWclNum: urgent,
          ybClzNum,
          yzClzNum,
          jjClzNum,
          ybDshNum,
          yzDshNum,
          jjDshNum,
          ybYclNum,
          yzYclNum,
          jjYclNum,
          ybYhlNum,
          yzYhlNum,
          jjYhlNum,
        } = data || {}
        setGeneralCount(general)
        setSeriousCount(serious)
        setUrgentCount(urgent)

        generalNumObj.ybWclNum = general
        generalNumObj.ybClzNum = ybClzNum
        generalNumObj.ybDshNum = ybDshNum
        generalNumObj.ybYclNum = ybYclNum
        generalNumObj.ybYhlNum = ybYhlNum

        seriousNumObj.yzWclNum = serious
        seriousNumObj.yzClzNum = yzClzNum
        seriousNumObj.yzDshNum = yzDshNum
        seriousNumObj.yzYclNum = yzYclNum
        seriousNumObj.yzYhlNum = yzYhlNum

        urgentNumObj.jjWclNum = urgent
        urgentNumObj.jjClzNum = jjClzNum
        urgentNumObj.jjDshNum = jjDshNum
        urgentNumObj.jjYclNum = jjYclNum
        urgentNumObj.jjYhlNum = jjYhlNum
      }
      catch (e) {}
    }

    async function updateNotificationInfoList() {
      try {
        const { data } = await getWarningMsg()
        setInfoList(data)
      }
      catch (e) {}
    }

    async function toggleClickNotificationButton() {
      if (!visible.value) {
        updateNotificationInfoList()
      }
      setVisible(!visible.value)
    }

    const route = useRoute()
    watch(
      () => route.fullPath,
      (val) => {
        if (val === '/warning-overview' || userStore.showWarningNotification) {
          updateNotificationCount()
        }
      },
      { immediate: true },
    )

    return {
      generalCount,
      seriousCount,
      urgentCount,
      infoList,
      visible,

      generalNumObj,
      seriousNumObj,
      urgentNumObj,
      generalTotal,
      seriousTotal,
      urgentTotal,

      updateNotificationCount,
      updateNotificationInfoList,
      toggleClickNotificationButton,
    }
  },
)
